package com.oa.framework.acc.account.service;

import com.alibaba.fastjson.JSON;
import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheInvalidateContainer;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.oa.common.exception.CreateObjectException;
import com.oa.common.identity.IdGenerateUtils;
import com.oa.framework.acc.account.dao.UserAccountDao;
import com.oa.framework.acc.account.domain.AccountPrincipal;
import com.oa.framework.acc.account.domain.UserAccount;
import com.oa.framework.acc.account.domain.UserSecurityInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author cuichao
 */
@Service
public class UserAccountServiceImpl implements UserAccountService {

    @Autowired
    private UserAccountDao accountDao;
    @Autowired
    private PasswordEncoder passwordEncoder;

    private String defaultPassword = "111111";

    @Override
    public IPage<UserAccount> searchAccountIncludePriorityPrincipal(IPage<UserAccount> page) {
        return accountDao.searchAccountIncludePriorityPrincipal(page);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public boolean createUserAccount(UserAccount account) throws CreateObjectException {
        final int createSuccessFlag = 1;
        //set password 设置密码
        String password = this.passwordEncoder.encode(defaultPassword);
        account.setPassword(password);
        //insert account 插入账户基本信息
        int flag = this.accountDao.insert(account);
        if (flag != createSuccessFlag) {
            throw new CreateObjectException();
        }
        //insert PriorityPrincipal 插入用户主要身份信息
        AccountPrincipal priority = account.getPriorityPrincipal();
        priority.setPrincipalId(Long.valueOf(IdGenerateUtils.snowflake()));
        flag = this.accountDao.addUserAccountPrincipal(account.getAccountId(), priority);
        if (flag != createSuccessFlag) {
            throw new CreateObjectException();
        }
        //insert priorityId 为账号基本信息设置主要身份ID
        flag = this.accountDao.setAccountPriorityPrincipal(account.getAccountId(), priority.getPrincipalId());
        if (flag != createSuccessFlag) {
            throw new CreateObjectException();
        }
        return true;
    }


    @Override
    public boolean updateUserAccount(UserAccount account) {
        final int updateSuccessFlag = 1;
        int flag = this.accountDao.updateUserAccountSecurityInfo(account);
        if (flag == updateSuccessFlag) {
            return true;
        }
        return false;
    }


    @Override
    public AccountPrincipal addUserAccountPrincipal(Long accountId, AccountPrincipal principal) throws CreateObjectException {
        final int successFlag = 1;
        principal.setPrincipalId(Long.valueOf(IdGenerateUtils.snowflake()));
        int flag = this.accountDao.addUserAccountPrincipal(accountId, principal);
        if (flag != successFlag) {
            throw new CreateObjectException();
        }
        return principal;
    }

    @Override
    public boolean updateUserAccountPrincipal(Long accountId, AccountPrincipal principal) {
        final int successFlag = 1;
        int flag = this.accountDao.updateUserAccountPrincipal(accountId, principal);
        if (flag == successFlag) {
            return true;
        }
        return false;
    }


    @Override
    public boolean removeAccountPrincipal(Long accountId, Long principalId) {
        final int successFlag = 1;
        int flag = this.accountDao.removeUserAccountPrincipal(accountId, principalId);
        if (flag == successFlag) {
            return true;
        }
        return false;
    }

    @Override
    public UserAccount getUserAccountIncludePrincipalsByAccountId(Long accountId) {
        return accountDao.getUserAccountIncludePrincipalsByAccountId(accountId);
    }


    @Override
    public UserSecurityInfo loadUserAccountSecurityInfo(String username) {
        return accountDao.loadUserAccountSecurityInfo(username);
    }


}
